package com.pan.admin.module.channel.service.impl;

import com.gitee.apanlh.util.valid.Assert;
import com.pan.admin.cache.DistrictCache;
import com.pan.admin.common.utils.DateUtils;
import com.pan.admin.common.utils.SecurityUtils;
import com.pan.admin.common.utils.StringUtils;
import com.pan.admin.module.channel.dao.LoanChannelFlowConfigDao;
import com.pan.admin.module.channel.dao.LoanChannelFlowConfigIpCityDao;
import com.pan.admin.module.channel.dao.LoanChannelFlowConfigIpOffModuleDao;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfig;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfigIPCityPO;
import com.pan.admin.module.channel.entity.LoanChannelFlowConfigIpOffModule;
import com.pan.admin.module.channel.service.LoanChannelFlowConfigService;
import com.pan.admin.module.common.entity.DistrictPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))Service业务层处理
 *
 * @author ts
 * @date 2023-09-27
 */
@Service
public class LoanChannelFlowConfigServiceImpl implements LoanChannelFlowConfigService {

    @Autowired
    private LoanChannelFlowConfigDao loanChannelFlowConfigDao;
    @Autowired
    private LoanChannelFlowConfigIpCityDao loanChannelFlowConfigIpCityDao;
    @Autowired
    private LoanChannelFlowConfigIpOffModuleDao loanChannelFlowConfigIpOffModuleDao;

    /**
     * 根据ID获取列表渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     *
     * @param id 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))主键
     * @return 渠道流程控制配置(动画 、 是否开启验证码 、 开启协议 、 隐私协议等等))
     */
    @Override
    public LoanChannelFlowConfig getById(Long id) {
        return loanChannelFlowConfigDao.getById(id);
    }

    /**
     * 查询渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))列表
     *
     * @param loanChannelFlowConfig 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     * @return 渠道流程控制配置(动画 、 是否开启验证码 、 开启协议 、 隐私协议等等))
     */
    @Override
    public List<LoanChannelFlowConfig> list(LoanChannelFlowConfig loanChannelFlowConfig) {
        return loanChannelFlowConfigDao.list(loanChannelFlowConfig);
    }

    /**
     * 新增渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     *
     * @param loanChannelFlowConfig 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     * @return 结果
     */
    @Override
    public int insertLoanChannelFlowConfig(LoanChannelFlowConfig loanChannelFlowConfig) {
        loanChannelFlowConfig.setCreateTime(DateUtils.getNowDate());
        loanChannelFlowConfig.setCreateBy(SecurityUtils.getUsername());
        return loanChannelFlowConfigDao.insertLoanChannelFlowConfig(loanChannelFlowConfig);
    }

    /**
     * 修改渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     *
     * @param loanChannelFlowConfig 渠道流程控制配置(动画、是否开启验证码、开启协议、隐私协议等等))
     * @return 结果
     */
    @Override
    public int updateLoanChannelFlowConfig(LoanChannelFlowConfig loanChannelFlowConfig) {
        loanChannelFlowConfig.setUpdateTime(DateUtils.getNowDate());
        loanChannelFlowConfig.setUpdateBy(SecurityUtils.getUsername());
        return loanChannelFlowConfigDao.updateLoanChannelFlowConfig(loanChannelFlowConfig);
    }

    @Override
    @Transactional(readOnly = true)
    public List<LoanChannelFlowConfigIPCityPO> listIpCity(Long loanChannelId) {
        return loanChannelFlowConfigIpCityDao.list(loanChannelId);
    }

    @Override
    public void batchInsertIpCity(Long loanChannelId, String areaCodes) {
        //  删除原有关联项
        loanChannelFlowConfigIpCityDao.delete(loanChannelId);

        // 关联计划地区
        if (StringUtils.isNotEmpty(areaCodes)) {
            List<LoanChannelFlowConfigIPCityPO> list = new ArrayList<>();

            String[] idsArray = areaCodes.split(",");
            for (int i = 0; i < idsArray.length; i++) {
                DistrictPO area = DistrictCache.getByCode(idsArray[i]);
                Assert.isNotNullThrows(area, new RuntimeException("错误地区"));

                LoanChannelFlowConfigIPCityPO loanChannelFlowConfigIPCityPO = new LoanChannelFlowConfigIPCityPO();
                loanChannelFlowConfigIPCityPO.setLoanChannelId(loanChannelId);
                loanChannelFlowConfigIPCityPO.setAreaCode(idsArray[i]);
                loanChannelFlowConfigIPCityPO.setAreaName(area.getName());
                loanChannelFlowConfigIPCityPO.setCreateBy(SecurityUtils.getUsername());
                loanChannelFlowConfigIPCityPO.setCreateTime(DateUtils.getNowDate());
                list.add(loanChannelFlowConfigIPCityPO);
            }
            loanChannelFlowConfigIpCityDao.batchInsert(list);
        }
    }

    @Override
    @Transactional(readOnly = true)
    public List<LoanChannelFlowConfigIpOffModule> listIpOffModule(Long loanChannelId) {
        return loanChannelFlowConfigIpOffModuleDao.list(loanChannelId);
    }

    @Override
    public void batchInsertIpOffModule(Long loanChannelId, String areaCodes) {
        //  删除原有关联项
        loanChannelFlowConfigIpOffModuleDao.delete(loanChannelId);

        // 关联计划地区
        if (StringUtils.isNotEmpty(areaCodes)) {
            List<com.pan.admin.module.channel.entity.LoanChannelFlowConfigIpOffModule> list = new ArrayList<>();

            String[] idsArray = areaCodes.split(",");
            for (String id : idsArray) {
                DistrictPO area = DistrictCache.getByCode(id);
                Assert.isNotNullThrows(area, new RuntimeException("错误地区"));

                LoanChannelFlowConfigIpOffModule loanChannelFlowConfigIpOffModule = new LoanChannelFlowConfigIpOffModule();
                loanChannelFlowConfigIpOffModule.setLoanChannelId(loanChannelId);
                loanChannelFlowConfigIpOffModule.setAreaCode(id);
                loanChannelFlowConfigIpOffModule.setAreaName(area.getName());
                loanChannelFlowConfigIpOffModule.setCreateBy(SecurityUtils.getUsername());
                loanChannelFlowConfigIpOffModule.setCreateTime(DateUtils.getNowDate());
                list.add(loanChannelFlowConfigIpOffModule);
            }
            loanChannelFlowConfigIpOffModuleDao.batchInsert(list);
        }
    }
}
